<?

/**
 * DevSuite
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://suite.rapidcodefactory.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@rapidcodefactory.com so we can send you a copy immediately.
 *
 * @category   RFC_Model
 * @package    RCF_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
 * @version    
*/



/**
 * Class for DB_Object Model
 *
 * @category   RFC_Model
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class Orders extends DomainModel {
	protected $id;	//primaryKey=yes, dataType=int, maxLength=, allowNulls=no
	protected $appsId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=no
	protected $usersId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=yes
	protected $lastStatusesId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=yes
	protected $shippingServicesId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=no
	protected $orderDate;	//primaryKey=no, dataType=timestamp, maxLength=, allowNulls=no
	protected $saddrFirstName;	//primaryKey=no, dataType=varchar, maxLength=20, allowNulls=no
	protected $saddrLastName;	//primaryKey=no, dataType=varchar, maxLength=30, allowNulls=no
	protected $saddrEmail;	//primaryKey=no, dataType=varchar, maxLength=75, allowNulls=no
	protected $saddrStreet;	//primaryKey=no, dataType=varchar, maxLength=50, allowNulls=yes
	protected $saddrBuildingNo;	//primaryKey=no, dataType=varchar, maxLength=10, allowNulls=yes
	protected $saddrApartmentNo;	//primaryKey=no, dataType=varchar, maxLength=10, allowNulls=yes
	protected $saddrPostcode;	//primaryKey=no, dataType=varchar, maxLength=15, allowNulls=yes
	protected $saddrCity;	//primaryKey=no, dataType=varchar, maxLength=50, allowNulls=yes
	protected $saddrCountry;	//primaryKey=no, dataType=varchar, maxLength=50, allowNulls=yes
	protected $baddrCompanyName;	//primaryKey=no, dataType=varchar, maxLength=50, allowNulls=yes
	protected $baddrFirstName;	//primaryKey=no, dataType=varchar, maxLength=20, allowNulls=yes
	protected $baddrLastName;	//primaryKey=no, dataType=varchar, maxLength=30, allowNulls=yes
	protected $baddrEmail;	//primaryKey=no, dataType=varchar, maxLength=50, allowNulls=yes
	protected $baddrStreet;	//primaryKey=no, dataType=varchar, maxLength=50, allowNulls=yes
	protected $baddrBuildingNo;	//primaryKey=no, dataType=varchar, maxLength=10, allowNulls=yes
	protected $baddrApartmentNo;	//primaryKey=no, dataType=varchar, maxLength=10, allowNulls=yes
	protected $baddrPostcode;	//primaryKey=no, dataType=varchar, maxLength=15, allowNulls=yes
	protected $baddrCity;	//primaryKey=no, dataType=varchar, maxLength=50, allowNulls=yes
	protected $baddrCountry;	//primaryKey=no, dataType=varchar, maxLength=50, allowNulls=yes
	protected $baddrVatId;	//primaryKey=no, dataType=varchar, maxLength=100, allowNulls=yes
	protected $remarks;	//primaryKey=no, dataType=varchar, maxLength=500, allowNulls=yes
	protected $sessionId;	//primaryKey=no, dataType=varchar, maxLength=500, allowNulls=yes
}




/**
 * Class for DB_Object ModelMapper
 *
 * @category   RFC_Model
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class OrdersMapper extends ModelMapper {
	function __construct(){
		parent::__construct();
		$this->tableName = 'orders';
		$this->selectViewName = 'v_orders';
		$this->selectFields = '
				id,
				apps_id,
				users_id,
				last_statuses_id,
				shipping_services_id,
				order_date,
				saddr_first_name,
				saddr_last_name,
				saddr_email,
				saddr_street,
				saddr_building_no,
				saddr_apartment_no,
				saddr_postcode,
				saddr_city,
				saddr_country,
				baddr_company_name,
				baddr_first_name,
				baddr_last_name,
				baddr_email,
				baddr_street,
				baddr_building_no,
				baddr_apartment_no,
				baddr_postcode,
				baddr_city,
				baddr_country,
				baddr_vat_id,
				remarks,
				session_id
		';

		$this->findQuery = self::$DB->prepare("
			SELECT ".$this->selectFields."
			FROM ".$this->selectViewName."
			WHERE id = ?
		");
		$this->insertQuery = self::$DB->prepare("
			CALL p_add_orders (@p_id,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?
		)");


		$this->updateQuery = self::$DB->prepare("
			CALL p_upd_orders (
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?

		)");

		$this->deleteQuery = self::$DB->prepare("
			CALL p_del_orders (?)
		");

		$this->cacheTime = null;
	}

	protected function doLoad($array){
		$acuire = str_replace('Mapper', '', get_class($this));
		$obj = new $acuire($array['id']);
		$obj->setId($array['id']);
		$obj->setAppsId($array['apps_id']);
		$obj->setUsersId($array['users_id']);
		$obj->setLastStatusesId($array['last_statuses_id']);
		$obj->setShippingServicesId($array['shipping_services_id']);
		$obj->setOrderDate($array['order_date']);
		$obj->setSaddrFirstName($array['saddr_first_name']);
		$obj->setSaddrLastName($array['saddr_last_name']);
		$obj->setSaddrEmail($array['saddr_email']);
		$obj->setSaddrStreet($array['saddr_street']);
		$obj->setSaddrBuildingNo($array['saddr_building_no']);
		$obj->setSaddrApartmentNo($array['saddr_apartment_no']);
		$obj->setSaddrPostcode($array['saddr_postcode']);
		$obj->setSaddrCity($array['saddr_city']);
		$obj->setSaddrCountry($array['saddr_country']);
		$obj->setBaddrCompanyName($array['baddr_company_name']);
		$obj->setBaddrFirstName($array['baddr_first_name']);
		$obj->setBaddrLastName($array['baddr_last_name']);
		$obj->setBaddrEmail($array['baddr_email']);
		$obj->setBaddrStreet($array['baddr_street']);
		$obj->setBaddrBuildingNo($array['baddr_building_no']);
		$obj->setBaddrApartmentNo($array['baddr_apartment_no']);
		$obj->setBaddrPostcode($array['baddr_postcode']);
		$obj->setBaddrCity($array['baddr_city']);
		$obj->setBaddrCountry($array['baddr_country']);
		$obj->setBaddrVatId($array['baddr_vat_id']);
		$obj->setRemarks($array['remarks']);
		$obj->setSessionId($array['session_id']);

		return $obj;
	}

	function insert(DomainModel $obj){

		$values = array(
			$obj->getAppsId(),
			$obj->getUsersId(),
			$obj->getLastStatusesId(),
			$obj->getShippingServicesId(),
			$obj->getOrderDate(),
			$obj->getSaddrFirstName(),
			$obj->getSaddrLastName(),
			$obj->getSaddrEmail(),
			$obj->getSaddrStreet(),
			$obj->getSaddrBuildingNo(),
			$obj->getSaddrApartmentNo(),
			$obj->getSaddrPostcode(),
			$obj->getSaddrCity(),
			$obj->getSaddrCountry(),
			$obj->getBaddrCompanyName(),
			$obj->getBaddrFirstName(),
			$obj->getBaddrLastName(),
			$obj->getBaddrEmail(),
			$obj->getBaddrStreet(),
			$obj->getBaddrBuildingNo(),
			$obj->getBaddrApartmentNo(),
			$obj->getBaddrPostcode(),
			$obj->getBaddrCity(),
			$obj->getBaddrCountry(),
			$obj->getBaddrVatId(),
			$obj->getRemarks(),
			$obj->getSessionId()
		);

		$this->doStatement($this->insertQuery, $values);
		$obj->setId(self::$DB->getOne("SELECT @p_id"));
	}

	function update(DomainModel $obj){
		$values = array(
			$obj->getId(),
			$obj->getAppsId(),
			$obj->getUsersId(),
			$obj->getLastStatusesId(),
			$obj->getShippingServicesId(),
			$obj->getOrderDate(),
			$obj->getSaddrFirstName(),
			$obj->getSaddrLastName(),
			$obj->getSaddrEmail(),
			$obj->getSaddrStreet(),
			$obj->getSaddrBuildingNo(),
			$obj->getSaddrApartmentNo(),
			$obj->getSaddrPostcode(),
			$obj->getSaddrCity(),
			$obj->getSaddrCountry(),
			$obj->getBaddrCompanyName(),
			$obj->getBaddrFirstName(),
			$obj->getBaddrLastName(),
			$obj->getBaddrEmail(),
			$obj->getBaddrStreet(),
			$obj->getBaddrBuildingNo(),
			$obj->getBaddrApartmentNo(),
			$obj->getBaddrPostcode(),
			$obj->getBaddrCity(),
			$obj->getBaddrCountry(),
			$obj->getBaddrVatId(),
			$obj->getRemarks(),
			$obj->getSessionId()
		);

		$this->doStatement($this->updateQuery, $values);
	}

	function delete(DomainModel $obj){
		$this->doStatement($this->deleteQuery, array($obj->getId()));
	}

	function newId(){

		$row = self::$DB->getRow('SHOW TABLE STATUS LIKE orders');
		return $row['Auto_increment'];

	}
}


/**
 * Class for DB_Object Model
 *
 * @category   RFC_ModelCollection
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class OrdersCollection extends ModelCollection {
	function add(Orders $model){
		$this->doAdd($model);
	}
}


/**
 * Class for DB_Object Model
 *
 * @category   RFC_ModelCollectionExtended
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class OrdersCollectionExtended extends OrdersCollection {
	private $stmt;
	private $valueArray;
	private $mapper;
	private $run = false;

	function __construct(ModelMapper $mapper, $stmtHandle, $valueArray){
		parent::__construct();
		$this->stmt = $stmtHandle;
		$this->valueArray = $valueArray;
		$this->mapper = $mapper;
	}

	function notifyAccess(){
		if (!$this->run){
			$result = $this->mapper->doStatement($this->stmt, $this->valueArray,$this->mapper->getCacheTime());
			$this->init_db($result, $this->mapper);
			$this->run = true;
		}
	}
}

?>
